package com.kzd.gtgame.main.mine

import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.app.URL_PAY_COIN
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.entity.PlatformData
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.popup.PlatformBottomPopup
import com.kzd.gtgame.main.toMineRichTextActivityUrl
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_platform.*
import java.lang.StringBuilder

class MinePlatformActivity : OwnerViewModeActivity<MineViewModel>() {

    private val mPlatformBottomPopup: PlatformBottomPopup by lazy {
        PlatformBottomPopup(context = this) { isHave, money ->
            viewModel.minePlatformOrder(money.id.toString(),
                money.money.toString(),
                mPayType.toString(),
                if (isHave) 1 else 0)
        }
    }

    private var mPayType: Int = 2

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_platform

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flMinePlatformBar)
    }

    override fun initView() {

        tvMinePlatformSubmit.setOnClickAntiShakeListener {
            val data = viewModel.platformMoneyLiveData
                .value?.data?.data
            if (data?.data.isNullOrEmpty()) {
                viewModel.minePlatformMoney()
            } else {
                mPlatformBottomPopup.fillData(mPayType, data!!)
                    .showPopupWindow()
            }
        }
        rbMinePlatformWx.isChecked = true
        rbMinePlatformWx.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) mPayType = 2
        }
        rbMinePlatformAli.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) mPayType = 1
        }
        tvMinePlatformUsername.text = StringBuilder()
            .append("用户名：").append(SPSConstance.userInfo?.user_name)
        tvMinePlatformMoney.text = StringBuilder()
            .append("平台币：").append(SPSConstance.userMine?.coin ?: 0)
    }

    override fun initData() {
        viewModel.minePlatformMoney()
    }

    override fun onResume() {
        super.onResume()
        viewModel.userMine()
    }

    override fun initViewMode() {

        viewModel.platformMoneyLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })

        viewModel.platformOrderLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val url = StringBuilder(URL_PAY_COIN)
                .append("&id=").append(data).toString()
            toMineRichTextActivityUrl(
                title = "支付", url = url
            )
        })

        viewModel.userMineLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            tvMinePlatformUsername.text = StringBuilder()
                .append("用户名：").append(SPSConstance.userInfo?.user_name)
            tvMinePlatformMoney.text = StringBuilder()
                .append("平台币：").append(SPSConstance.userMine?.coin ?: 0)
        })
    }

    private fun fillData(data: PlatformData) {
        tvMinePlatformCount.text = StringBuilder("双倍卡数量：")
            .append(data.count)
    }
}